import { Component, OnInit, ViewChild } from '@angular/core';
import { UserService, UserQueryParameter } from '../../service/user.service';
import { PagerComponent } from '../../component/pager/pager.component';
import { MgUser } from '../../domain/mguser';
import { Toastr } from '../../common/toastr';
import { NavbarService } from '../../component/navbar/navbar.service';
import { ModalService } from '../../component/modal/modal.service';
import { MessageBoxResult } from '../../component/modal/messageBoxResult';

@Component({
    moduleId: module.id,
    templateUrl: 'mguser.component.html'
})
export class MguserComponent implements OnInit {
    private queryParams: UserQueryParameter = {
        currentPage: 1,
        pageSize: 10,
    };
    private title: string = "管理员管理";
    private subTitle: string = "查询管理员信息";
    private mguserList: MgUser[] = [];
    @ViewChild(PagerComponent)
    private pager: PagerComponent;

    constructor(
        private userSvr: UserService,
        private modal: ModalService
    ) {
        NavbarService.Current.OnAddData = () => {
            NavbarService.Current.Navigate(['/main/mguser/create']);
        }
    }

    ngOnInit() {
        this.getMgUserList();

        this.pager.OnPageIndexChanged = (pageIndex: number) => {
            this.queryParams.currentPage = pageIndex;
            this.getMgUserList();
        }
    }

    async getMgUserList() {
        try {
            let r = await this.userSvr.GetManageUserList(this.queryParams);
            this.mguserList = r.list;
            this.pager.Load(r.count);
        } catch (error) {
            Toastr.Error(error);
        }
    }

    getMguserInfo(id: number) {
        NavbarService.Current.Navigate(['/main/mguser/info', id]);
    }

    editMguser(id: number) {
        NavbarService.Current.Navigate(['/main/mguser/edit', id]);
    }

    async deleteMguser(item: MgUser) {
        try {
            let r = await this.modal.Show("提示消息", "是否要删除管理用户【" + item.userName + "】?");
            if (r == MessageBoxResult.Ok) {
                await this.userSvr.DeleteMgUser(item.mangerId);
                Toastr.Success("删除成功！");
            }
        } catch (error) {
            Toastr.Error(error);
        }
    }
}